import random
import time

import scrapy
import re
from ..items import ScrapysteamItem
from ..items import Mysqldb
from ..items import GameDet
import datetime



class SteamSpider(scrapy.Spider):
    name = 'steam'
    gameType = '休闲'
    # allowed_domains = ['https://store.steampowered.com']
    # start = input('请输入开始序号')
    # end = input('请输入结束编号')
    start = 9000
    end = int(start) + 999
    mysqldb = Mysqldb()



    urls = mysqldb.get_appId('strategygame', start, end)
    start_urls = []
    for url in urls:
        template = 'https://store.steampowered.com/app/'
        start_urls.append(template + url[0] + '/_/')
    print(start_urls)

    def parse(self, response):
        time.sleep(random.random() * 3)
        game = GameDet()
        # get appId
        game['appId'] = response.xpath('//div[@class="glance_tags popular_tags"]/@data-appid').extract()[0]

        # get name
        game['name'] = response.xpath('//*[@id="appHubAppName"]/text()').extract()[0]

        # get Developers and Publishers
        developersList = response.xpath('//div[@id = "developers_list"]/a/text()').extract()
        # this list of A contain developer and publisher,use list of A sub list of developer
        listOfAll = response.xpath('//div[@class = "summary column"]/a/text()').extract()
        game['developers'] = "|".join(developersList)
        publishersList = listOfAll[-(len(listOfAll) - len(developersList)):]
        game['publishers'] = "|".join(publishersList)

        # get Issue date
        date = response.xpath('//div[@class = "date"]/text()').extract()[0]
        issueDate = datetime.datetime.strptime(date, '%d %b, %Y')
        game['issueDate'] = issueDate

        # get price
        item = 0.0
        if len(response.xpath('//div[@class = "discount_original_price"]/text()').extract()):
            price = response.xpath('//div[@class = "discount_original_price"]/text()').extract()[0]
            item = float(re.findall(r"\d+\.?\d*", price)[0])
        elif len(response.xpath('//div[@class = "game_purchase_price price"]/text()').extract()):
            price = response.xpath('//div[@class = "game_purchase_price price"]/text()').extract()[0]
            try:
                item = float(re.findall(r"\d+\.?\d*", price)[0])
            except Exception as e:
                item = 0.0
        game['price'] = item

        # get review
        try:
            review = response.xpath('//div[@class = "summary column"]/span[1]/text()').extract()
            review = review[0]
            detailInformation = response.xpath('//div[@class = "summary column"]/span[3]/text()').extract()[0]
            if len(detailInformation):
                favorableRate = re.findall(r"\d+\.?\d*", detailInformation)[0]
                numOfComments = re.findall(r"\d+\.?\d*", detailInformation)[1]
            else:
                numOfComments = str(0)
                favorableRate = str(0)
        except Exception as e:
            review = 'not_enough_review'
            temp = response.xpath('//div[@class = "summary column"]/span[1]/text()').extract()[0]
            numOfComments = re.findall(r"\d+\.?\d*", temp)[0]
            favorableRate = 0
        game['review'] = review
        game['numOfComments'] = numOfComments
        game['favorableRate'] = favorableRate

        # get app tag
        temp = response.xpath('//a[@class = "app_tag"]/text()').extract()
        tagList = [' '.join([i.strip() for i in price.strip().split('\t')]) for price in temp]
        appTag = "|".join(tagList)
        game['appTag'] = appTag

        yield game
